<?php
/**
* @file $Id: Configuration.php 590 2007-07-12 19:29:59Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$user_syear    = UserSyear();
$user_school   = UserSchool();
$user_username = User('USERNAME');

if ($_REQUEST['values']) {
	DBQuery("
			DELETE FROM  PROGRAM_USER_CONFIG 
			WHERE        USERNAME = '$user_username' 
			 AND         PROGRAM = 'Gradebook'
			");
			
	foreach ($_REQUEST['values'] as $title => $value) {
		DbInsertInto("PROGRAM_USER_CONFIG"
				, array( 'USERNAME' => $user_username
                       , 'PROGRAM'  => 'GradeBook'
                       , 'TITLE'    => $title
                       , 'VALUE'    => $value
					   ) );
					   
		DBQuery("
				INSERT INTO  PROGRAM_USER_CONFIG 
				( USERNAME
				, PROGRAM
				, TITLE
				, VALUE
				) VALUES
				( '$user_username'
				, 'Gradebook'
				, '$title'
				, '".str_replace("\'","''",str_replace('%','',$value))."'
				)
				");
	}
}

$config_RET = DBGet(DBQuery("
		SELECT       TITLE
		 ,           VALUE 
		FROM         PROGRAM_USER_CONFIG 
		WHERE        USERNAME = '$user_username' 
		 AND         PROGRAM = 'Gradebook'
		"), array(), array('TITLE'));

if (count($config_RET)) {
	foreach ($config_RET as $title => $value) {
		$programconfig[$title] = $value[1]['VALUE'];
	}
}

// get some defaults if they dont yet have settings
if (!$programconfig['ROUNDING']) {
	$programconfig['ROUNDING'] = 'NORMAL';
}

if (!$programconfig['ASSIGNMENT_SORTING']) {
	$programconfig['ASSIGNMENT_SORTING'] = 'ASSIGNMENT_ID';
}

// some preloaded stuff so we dont have to junk up the html generating code with calculations
$check_if_round_up = (($programconfig['ROUNDING'] == 'UP')?'CHECKED':'');
$check_if_round_down = (($programconfig['ROUNDING'] == 'DOWN')?'CHECKED':'');
$check_if_round_normal = (($programconfig['ROUNDING'] == 'NORMAL')?'CHECKED':'');
$check_if_sort_by_id = (($programconfig['ASSIGNMENT_SORTING'] == 'ASSIGNMENT_ID')?'CHECKED':'');
$check_if_sort_by_due_date = (($programconfig['ASSIGNMENT_SORTING'] == 'DUE_DATE')?'CHECKED':'');
$check_if_sort_by_assign_date = (($programconfig['ASSIGNMENT_SORTING'] == 'ASSIGNED_DATE')?'CHECKED':'');
$check_if_weighted = (($programconfig['WEIGHT'] == 'Y')?'CHECKED':'');
$check_if_eligibility_cumulative = (($programconfig['ELIGIBILITY_CUMULITIVE'] == 'Y')?'CHECKED':'');

$semesters = DBGet(DBQuery("
		SELECT        TITLE
		 ,            MARKING_PERIOD_ID 
		FROM          SCHOOL_SEMESTERS 
		WHERE         SYEAR = '$user_syear' 
		 AND          SCHOOL_ID = '$user_school' 
		ORDER BY      SORT_ORDER
		"));
		
$quarters = DBGet(DBQuery("
		SELECT        TITLE
		 ,            MARKING_PERIOD_ID 
		FROM          SCHOOL_QUARTERS
		WHERE         SYEAR = '$user_syear' 
		 AND          SCHOOL_ID = '$user_school' 
		ORDER BY      SORT_ORDER
		"), array(), array('SEMESTER_ID'));
		
$grades = DBGet(DBQuery("
		SELECT        cp.TITLE AS CP_TITLE
		 ,            c.TITLE AS COURSE_TITLE
		 ,            cp.COURSE_PERIOD_ID
		 ,            rcg.TITLE
		 ,            rcg.ID
		 ,            rcg.DEFAULT_BREAKOFF 
		FROM          REPORT_CARD_GRADES rcg
		 ,            COURSE_PERIODS cp
		 ,            COURSES c
		 ,            SCHOOL_PERIODS sp 
		WHERE         cp.COURSE_ID = c.COURSE_ID 
		 AND          cp.PERIOD_ID = sp.PERIOD_ID 
		 AND          cp.TEACHER_ID = '".User('STAFF_ID')."' 
		 AND          cp.SCHOOL_ID = rcg.SCHOOL_ID 
		 AND          cp.SYEAR = rcg.SYEAR 
		 AND          cp.SYEAR = '".UserSyear() . "' 
		 AND          rcg.SCALE_ID = cp.GRADE_SCALE_ID 
		ORDER BY      sp.SORT_ORDER
		 ,            rcg.SORT_ORDER
		"), array(), array('COURSE_PERIOD_ID'));

// fill $grades with defaults as needed
foreach ($grades as $course_period_id => $cp_grades) {
	$custom = false;
	foreach ($cp_grades as $grade) {
		if ($programconfig[$course_period_id . '-' . $grade['ID']]) {
			$custom = true;
			break;
		}
	}

	if (!$custom) {
		foreach ($cp_grades as $grade) {
			$programconfig[$course_period_id . '-' . $grade['ID']] // =
					= $grade['DEFAULT_BREAKOFF'];
		}
	}	
}
		
// start building the page
echo "
<form action='Modules.php?modname=$_REQUEST[modname]' 
		method='POST'
>";

if ($_REQUEST['modname'] != 'School_Setup/SystemPreferences.php') {
	DrawHeader(_('Gradebook') . ' - ' . ProgramTitle());
	DrawHeader('',"
	<input type='submit' 
			value='" . _('Save') . "'
	/>");
}

echo "
	<br/>";
	
PopTable('header', _('Configuration'));

echo "
	<table>
		<tr>
			<td colspan='3'>				
			</td>
		</tr>
		<tr>
			<td width='1'>
			</td>
			<td>
				<fieldset>
					<legend><b>" . _('General') . "</b></legend>
					<table>
						<tr>
							<td colspan='2'>" . DrawRoundedRect("
								<table>
									<tr>
										<td	colspan='3'>
											<font	color='white'	size='-2'>
												<B>" . _('Score Rounding') . "</B>
											</font>
										</td>
									</tr>
									<tr>
										<td>
											<input	type='radio'
													name='values[ROUNDING]'
													value='UP'
													$check_if_round_up
											/>
											<font	color='white'	size='-1'>
												" . _('Up') . "
											</font>
										</td>
										<td>
											<input	type='radio'
													name='values[ROUNDING]'
													value='DOWN'
													$check_if_round_down
											/>
											<font	color='white'	size='-1'>
												" . _('Down') . "
											</font>
										</td>
										<td>
											<input	type='radio'
													name='values[ROUNDING]'
													value='NORMAL'
													$check_if_round_normal
											/>
											<font	color='white'	size='-1'>
												" . _('Normal') . "
											</font>
										</td>
									</tr>
								</table>")."
							</td>
						</tr>
						<tr>
							<td	colspan='2'>" . DrawRoundedRect("
								<table>
									<tr>
										<td	colspan='3'>
											<font	color='white'	size='-2'>
												<B>" . _('Assignment Sorting') . "</B>
											</font>
										</td>
									</tr>
									<tr>
										<td>
											<input	type=radio	name=values[ASSIGNMENT_SORTING]	value=ASSIGNMENT_ID	$check_if_sort_by_id	/>
											<font	color='white'	size='-1'>
												" . _('Newest First') . "
											</font>
										</td>
										<td>
											<input	type='radio'
													name='values[ASSIGNMENT_SORTING]'
													value='DUE_DATE'
													$check_if_sort_by_due_date
											/>
											<font	color='white'	size='-1'>
												" . _('Due Date') . "
											</font>
										</td>
										<td>
											<input	type='radio'
													name='values[ASSIGNMENT_SORTING]'
													value='ASSIGNED_DATE'
													$check_if_sort_by_assign_date
											/>
											<font	color='white'	size='-1'>
												" . _('Assigned Date') . "
											</font>
										</td>
									</tr>
								</table>") . "
							</td>
						</tr>
						<tr>
							<td valign='top' width='30'>
							</td>
							<td>
								<input type='checkbox' 
										name='values[WEIGHT]' 
										value='Y'
										$check_if_weighted
								/>
								" . _('Weight Grades') . "
							</td>
						</tr>
						<tr>
							<td valign='top' width='30'>
							</td>
							<td>
								<input type='checkbox' 
										name='values[ELIGIBILITY_CUMULITIVE]' 
										value='Y'
										$check_if_eligibility_cumulative
								/>
								" 
			. _('Calculate Eligibility using Cumulative Semester Grades') . "
							</td>
						</tr>
					</table>
				</fieldset>
			</td>
			<td width='1'>
			</td>
		</tr>
		<tr>
			<td colspan='3'> </td>
		</tr>
		<tr>
			<td width='1'> </td>
			<td>
				<fieldset>
					<legend>
						<b>" . _('Score Breakoff Points') . "</b>
					</legend>
					<table cellspacing='1'>
						<tr>
							<td>";

foreach ($grades as $course_period_id => $cp_grades) {
	$table = "
								<table>
									<tr>
										<td	rowspan='2'	align='" . ALIGN_RIGHT . "'	width='100'>
											<font	color='white'	size='-1'>
												" . $cp_grades[1]['COURSE_TITLE']
												. " - "
												. substr($cp_grades[1]['CP_TITLE']
														, 0
														, strrpos(str_replace(' - '
																, ' ^ '
																, $cp_grades[1]['CP_TITLE'])
																, '^'
															)
												) . "
											</font>
										</td>";
										
	foreach ($cp_grades as $grade) {
		$table .= "
										<td>
											<b>
												<font	color='white'>
													$grade[TITLE]
												</font>
											</b>
										</td>";
	}
	
	$table .= "
									</tr>
									<tr>";
									
	foreach ($cp_grades as $grade) {
		$table .= "
										<td>
											<input	type='text'
													name='values[" . $course_period_id
																. '-'
																. $grade['ID']
																. "]'	
													value='" . $programconfig[
																  $course_period_id
																. '-' 
																. $grade['ID']]
																. "'
													size='3'
													maxlength='5'
											/>
										</td>";
	}
	
	$table .= "
									</tr>
								</table>";
	
	echo DrawRoundedRect($table);
	echo "
							</td>
						</tr>
						<tr>
							<td>";
}
echo "
							</td>
						</tr>
					</table>
				</fieldset>
			</td>
			<td width='1'></td>
		</tr>
		<tr>
			<td colspan='3'></td>
		</tr>
		<tr>
			<td width='1'></td>
			<td>
				<fieldset>
					<legend>
						<b>" . _('Final Grading Percentages') . "</b>
					</legend>
					<table cellspacing='1'>
						<tr>
							<td>";
							
foreach ($semesters as $sem)
{
	$table = "
								<table>
									<tr>
										<td	rowspan='2'	valign='middle'>
											<font	color='white'	size='-1'>
												$sem[TITLE]
											</font>
										</td>";
										
	foreach ($quarters[$sem['MARKING_PERIOD_ID']] as $qtr) {
		$table .= "
										<td>
											<font	color='white'>$qtr[TITLE]
											</font>
										</td>";
	}
	
	$table .= "
										<td>
											<font	color='white'>
												$sem[TITLE]	" . _('Exam') . "
											</font>
										</td>
									</tr>
									<tr>";
									
	foreach ($quarters[$sem['MARKING_PERIOD_ID']] as $qtr) {
		$table .= "
										<td>
											<input	type='text'
													name='values[$qtr[MARKING_PERIOD_ID]]
													value='"
														. $programconfig[$qtr['MARKING_PERIOD_ID']]
														. "'
													size='3'
													maxlength='3'
											/>
										</td>";
	}
	
	$table .= "
										<td>
											<input	type='text'
													name='values[E".$sem['MARKING_PERIOD_ID']."]'
													value='"
														. $programconfig['E'.$sem['MARKING_PERIOD_ID']]
														. "'
													size='3'
													maxlength='3'
											/>
										</td>
									</tr>
								</table>";

	echo DrawRoundedRect($table);
	echo "
							</td>
						</tr>
						<tr>
							<td>";
}

echo "
							</td>
						</tr>
					</table>
				</fieldset>
			</td>
		</tr>";
		
if ($_REQUEST['modname'] != 'School_Setup/SystemPreferences.php') {
	echo "
		<tr>
			<td colspan='3' align='center'>
				<input	type='submit'
						value='" . _('Save') . "' />
			</td>
		</tr>";
}

echo "
	</table>";

PopTable('footer');

echo "
</form>";

?>